// preload.js
// 它拥有与Chrome扩展一样的沙盒。
const { contextBridge, ipcRenderer } = require('electron');

window.addEventListener('DOMContentLoaded', () => {
    const replaceText = (selector, text) => {
        const element = document.getElementById(selector);
        if (element) element.innerText = text;
    };

    for (const dependency of ['chrome', 'node', 'electron']) {
        replaceText(`${dependency}-version`, process.versions[dependency]);
    }
});

contextBridge.exposeInMainWorld('electronAPI', {
    // category related interfaces
    NewCategory: (params) => ipcRenderer.invoke('categories/NewCategory', params),
    GetCategories: (params) => ipcRenderer.invoke('categories/GetCategories', params),
    DeleteCategories: (params) => ipcRenderer.invoke('categories/DeleteCategories', params),
    UpdateCategory: (params) => ipcRenderer.invoke('categories/UpdateCategory', params),
    // conda related interfaces
    CondaEnvDetails: (params) => ipcRenderer.invoke('remotes/CondaEnvDetails', params),
    CondaEnvList: (params) => ipcRenderer.invoke('remotes/CondaEnvList', params),
    CondaEnvNew: (params) => ipcRenderer.invoke('remotes/CondaEnvNew', params),
    CondaEnvServer: (params) => ipcRenderer.invoke('remotes/CondaEnvServer', params),
    // dataset related interfaces
    ConvertDataset: (params) => ipcRenderer.invoke('datasets/ConvertDataset', params),
    CreateDataset: (params) => ipcRenderer.invoke('datasets/CreateDataset', params),
    DeleteDataset: (params) => ipcRenderer.invoke('datasets/DeleteDataset', params),
    DownloadDataset: (params) => ipcRenderer.invoke('datasets/DownloadDataset', params),
    GetDatasetCloudList: (params) => ipcRenderer.invoke('datasets/GetDatasetCloudList', params),
    GetDatasetFormat: (params) => ipcRenderer.invoke('datasets/GetDatasetFormat', params),
    GetDatasetInfo: (params) => ipcRenderer.invoke('datasets/GetDatasetInfo', params),
    GetDatasetList: (params) => ipcRenderer.invoke('datasets/GetDatasetList', params),
    UpdateDataset: (params) => ipcRenderer.invoke('datasets/UpdateDataset', params),
    // label data related interfaces
    AddFrames: (params) => ipcRenderer.invoke('labeldata/AddFrames', params),
    DeleteFrames: (params) => ipcRenderer.invoke('labeldata/DeleteFrames', params),
    LoadDataset: (params) => ipcRenderer.invoke('labeldata/LoadDataset', params),
    LoadImage: (params) => ipcRenderer.invoke('labeldata/LoadImage', params),
    LoadLabel: (params) => ipcRenderer.invoke('labeldata/LoadLabel', params),
    SaveLabel: (params) => ipcRenderer.invoke('labeldata/SaveLabel', params),
    // models related interface
    CreateLocalModel: (params) => ipcRenderer.invoke('models/CreateLocalModel', params),
    DeleteLocalModel: (params) => ipcRenderer.invoke('models/DeleteLocalModel', params),
    DeleteTrainRecord: (params) => ipcRenderer.invoke('models/DeleteTrainRecord', params),
    EvalStart: (params) => ipcRenderer.invoke('models/EvalStart', params),
    EvalStop: (params) => ipcRenderer.invoke('models/EvalStop', params),
    GetCloudModels: (params) => ipcRenderer.invoke('models/GetCloudModels', params),
    GetLocalModels: (params) => ipcRenderer.invoke('models/GetLocalModels', params),
    GetModelArch: (params) => ipcRenderer.invoke('models/GetModelArch', params),
    GetModelConfigs: (params) => ipcRenderer.invoke('models/GetModelConfigs', params),
    GetModelParams: (params) => ipcRenderer.invoke('models/GetModelParams', params),
    GetRecordEvalResults: (params) => ipcRenderer.invoke('models/GetRecordEvalResults', params),
    GetRecordVisConfig: (params) => ipcRenderer.invoke('models/GetRecordVisConfig', params),
    GetTrainRecords: (params) => ipcRenderer.invoke('models/GetTrainRecords', params),
    LoadEvalResults: (params) => ipcRenderer.invoke('models/LoadEvalResults', params),
    LoadTrainResults: (params) => ipcRenderer.invoke('models/LoadTrainResults', params),
    TrainStart: (params) => ipcRenderer.invoke('models/TrainStart', params),
    TrainStop: (params) => ipcRenderer.invoke('models/TrainStop', params),
    UpdateLocalModel: (params) => ipcRenderer.invoke('models/UpdateLocalModel', params),
    // pipeline related interfaces
    GetPipeConfig: (params) => ipcRenderer.invoke('pipeline/GetPipeConfig', params),
    GetPipelines: (params) => ipcRenderer.invoke('pipeline/GetPipelines', params),
    PipelineStart: (params) => ipcRenderer.invoke('pipeline/PipelineStart', params),
    PipelineStatus: (params) => ipcRenderer.invoke('pipeline/PipelineStatus', params),
    // python environments related interfaces
    GetPythonEnvs: (params) => ipcRenderer.invoke('pythonEnv/GetPythonEnvs', params),
    RunPythonScript: (params) => ipcRenderer.invoke('pythonEnv/RunPythonScript', params),
    // queue related interfaces
    ClearQueue: (params) => ipcRenderer.invoke('queue/ClearQueue', params),
    PeekQueue: (params) => ipcRenderer.invoke('queue/PeekQueue', params),
    // remote related interfaces
    AddRemote: (params) => ipcRenderer.invoke('remotes/AddRemote', params),
    DeleteRemote: (params) => ipcRenderer.invoke('remotes/DeleteRemote', params),
    GetRemoteInfo: (params) => ipcRenderer.invoke('remotes/GetRemoteInfo', params),
    GetRemoteList: (params) => ipcRenderer.invoke('remotes/GetRemoteList', params),
    RemoteAppController: (params) => ipcRenderer.invoke('remotes/RemoteAppController', params),
    RemoteConnect: (params) => ipcRenderer.invoke('remotes/RemoteConnect', params),
    RemoteDisconnect: (params) => ipcRenderer.invoke('remotes/RemoteDisconnect', params),
    RemoteExecutor: (params) => ipcRenderer.invoke('remotes/RemoteExecutor', params),
    RemoteInstall: (params) => ipcRenderer.invoke('remotes/RemoteInstall', params),
    RemoteSystemInfo: (params) => ipcRenderer.invoke('remotes/RemoteSystemInfo', params),
    RemoteTest: (params) => ipcRenderer.invoke('remotes/RemoteTest', params),
    UpdateRemote: (params) => ipcRenderer.invoke('remotes/UpdateRemote', params),
    // subset related interfaces
    CreateSubset: (params) => ipcRenderer.invoke('subsets/CreateSubset', params),
    GetSubsets: (params) => ipcRenderer.invoke('subsets/GetSubsets', params),
    UpdateSubset: (params) => ipcRenderer.invoke('subsets/UpdateSubset', params),
    // utility interfaces
    GetRootDirectory: (params) => ipcRenderer.invoke('utils/GetRootDirectory', params),
    GetSubDirectories: (params) => ipcRenderer.invoke('utils/GetSubDirectories', params),
    IsDirectoryExists: (params) => ipcRenderer.invoke('utils/IsDirectoryExists', params),
    LocalSystemInfo: (params) => ipcRenderer.invoke('utils/LocalSystemInfo', params),
    OpenDirectory: (params) => ipcRenderer.invoke('utils/OpenDirectory', params),
    PathJoin: (params) => ipcRenderer.invoke('utils/PathJoin', params),
    Ping: (params) => ipcRenderer.invoke('utils/Ping', params),
    ReadFile: (params) => ipcRenderer.invoke('utils/ReadFile', params),
    ToggleConsole: (params) => ipcRenderer.invoke('utils/ToggleConsole', params),
    // video related insterfaces
    ExtractFrames: (params) => ipcRenderer.invoke('video/ExtractFrames', params)
});
